from typing import List


class Solution:
    def arrayNesting(self, nums: List[int]) -> int:
        ans = 0
        for i in range(len(nums)):
            if nums[i] == -1:
                continue
            cnt = 0
            j = i
            while nums[j] != -1:
                tmp = j
                j = nums[j]
                nums[tmp] = -1
                cnt += 1
            ans = max(ans, cnt)
        return ans